//
//  SuperViewController.m
//  KIASupervisory
//
//  Created by tan_xin on 14-6-30.
//  Copyright (c) 2014年 MyCompany. All rights reserved.
//

#import "SuperViewController.h"

@interface SuperViewController () <MBProgressHUDDelegate>

@end

@implementation SuperViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.networkManager = [[NetworkManager alloc] init];
    
    [self initHUD];
    
    [self.tableView addPullToRefreshWithActionHandler:^{
        [self refreshViewByReloadData];
    }];
    [self.navigationController setNavigationBarHidden:YES];
    //在弹出的键盘上面加一个view来放置退出键盘的Done按钮
    UIToolbar * topViewV = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
    [topViewV setBarStyle:UIBarStyleDefault];
    UIButton * buttonV = [UIButton buttonWithType:UIButtonTypeCustom];
    buttonV.frame = CGRectMake(320-44, 3, 30, 26);
    [buttonV.titleLabel setFont:[UIFont systemFontOfSize:15]];
    [buttonV setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [buttonV setTitle:@"完成" forState:UIControlStateNormal];
    [buttonV setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [buttonV addTarget:self action:@selector(dismissKeyBoard) forControlEvents:UIControlEventTouchUpInside];
    [topViewV addSubview:buttonV];
    self.keyboardBar = topViewV;
    
    [self.tableView triggerPullToRefresh];

}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    self.view.backgroundColor = [UIColor colorWithRed:240.0/255.0 green:239.0/255.0 blue:245.0/255.0 alpha:1.0];
    
    if (self.showLeftBtn) {
        [self initLeftNavigationItem];
    }
    
    if (self.showRightBtn) {
        [self initRightNavigationItem];
    }
    
}

- (void)endRefresh {
    [self.tableView.pullToRefreshView stopAnimating];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)initHUD {
    self.HUD = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:self.HUD];
    self.HUD.delegate = self;
}

- (void)showAlertViewWithMessage:(NSString *)msg {
    if ([msg length] > 0) {
        _alertView = [[UIAlertView alloc] initWithTitle:@""
                                                message:msg
                                               delegate:self
                                      cancelButtonTitle:@"确定"
                                      otherButtonTitles:nil];
        _alertView.tag = 10001;
        [NSTimer scheduledTimerWithTimeInterval:0.9f
                                         target:self
                                       selector:@selector(performDismissAlert)
                                       userInfo:nil
                                        repeats:NO];
        [_alertView show];
    }
}

- (void)performDismissAlert {
    if (_alertView) {
        [_alertView dismissWithClickedButtonIndex:0 animated:NO];
        _alertView = nil;
    }
}

- (void)dismissKeyBoard {
    
}

//添加键盘监听事件
- (void)addKeyboardNotificationForTarget:(id)observer {
    //监听键盘切换
    [[NSNotificationCenter defaultCenter] addObserver:observer
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:observer
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
    //判断iphone版本,ios5中文键盘高度有改变，需要相应调整UI
#ifdef __IPHONE_5_0
    float version = [[[UIDevice currentDevice] systemVersion] floatValue];
    if (version >= 5.0) {
        [[NSNotificationCenter defaultCenter] addObserver:observer
                                                 selector:@selector(keyboardWillShow:)
                                                     name:UIKeyboardWillChangeFrameNotification
                                                   object:nil];
    }
#endif
}

- (void)keyboardWillShow:(NSNotification *)notification {}

- (void)keyboardWillHide:(NSNotification *)notification {}

- (void)initLeftNavigationItem {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    self.leftBtn = button;
    button.frame = CGRectMake(10, 7, 30, 30);
    [button setBackgroundImage:[UIImage imageNamed:@"Navigationbar_btn_侧滑_n.png"] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:@"Navigationbar_btn_侧滑_p.png"] forState:UIControlStateHighlighted];
    [button addTarget:self action:@selector(leftNavItemAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = leftItem;
}

- (void)initRightNavigationItem {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    self.rightBtn = button;
    button.frame = CGRectMake(280, 7, 30, 30);
    [button setBackgroundImage:[UIImage imageNamed:@"Navigationbar_btn_确认_n.png"] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:@"Navigationbar_btn_确认_p.png"] forState:UIControlStateHighlighted];
    [button addTarget:self action:@selector(rightNavItemAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.rightBarButtonItem = rightItem;
}

- (void)leftNavItemAction:(id)sender {}

- (void)rightNavItemAction:(id)sender {}

- (void)refreshViewByReloadData {}

#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    if (alertView.tag == 10001) {
        [alertView dismissWithClickedButtonIndex:0 animated:NO];
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
